package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    private static final String View_Main = "/WEB-INF/jsp/catalog/Main.jsp";
    private static final String VIEW_REGISTER = "/WEB-INF/jsp/account/NewAccountForm.jsp";
    Account account = null;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AccountService accountService = new AccountService();
        HttpSession session = req.getSession();

//获得输入的验证码值
        String value1=req.getParameter("vCode");
        /*获取图片的值*/
        String value2=(String)session.getAttribute("checkcode");
        Boolean isSame = false;
        /*对比两个值（字母不区分大小写）*/
        if(value2.equalsIgnoreCase(value1)){
            isSame = true;
        }

        if(isSame){
            if (passwordsEqual(req, resp)){
                account = new Account();

                account.setUsername(req.getParameter("username"));
                account.setPassword(req.getParameter("password"));
                account.setLastName(req.getParameter("account.lastName"));
                account.setFirstName(req.getParameter("account.firstName"));
                account.setEmail(req.getParameter("account.email"));
                account.setPhone(req.getParameter("account.phone"));
                account.setAddress1(req.getParameter("account.address1"));
                account.setAddress2(req.getParameter("account.address2"));
                account.setCity(req.getParameter("account.city"));
                account.setState(req.getParameter("account.state"));
                account.setZip(req.getParameter("account.zip"));
                account.setCountry(req.getParameter("account.country"));

                account.setLanguagePreference(req.getParameter("account.languagePreference"));
                account.setFavouriteCategoryId(req.getParameter("account.favouriteCategoryId"));
                account.setListOption(Boolean.parseBoolean(req.getParameter("account.listOption")));
                account.setBannerOption(Boolean.parseBoolean(req.getParameter("account.bannerOption")));

                accountService.insertAccount(account);

                if (session.getAttribute("passwordNotEqualMSG")!=null){
                    session.setAttribute("passwordNotEqualMSG",null);
                }

                req.getRequestDispatcher(View_Main).forward(req,resp);
            }

            else {
                String passwordNotEqualMSG = "两次密码不一样！！！！";

                session.setAttribute("passwordNotEqualMSG",passwordNotEqualMSG);


                if(account != null){
                    HttpServletRequest httpRequest= req;
                    String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                            + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                    LogService logService = new LogService();

                    String logInfo = logService.logInfo(" ") + strBackUrl + " 确定订单";
                    logService.insertLogInfo(account.getUsername(), logInfo);
                }

                req.getRequestDispatcher(VIEW_REGISTER).forward(req,resp);
            }
        }else {
            String VcodeErroerMSG = "验证码错误！！！！";

            session.setAttribute("VcodeErroerMSG",VcodeErroerMSG);



            if(account != null){
                HttpServletRequest httpRequest= req;
                String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                LogService logService = new LogService();

                String logInfo = logService.logInfo(" ") + strBackUrl + account.getUsername()+"注册";
                logService.insertLogInfo(account.getUsername(), logInfo);
            }

            req.getRequestDispatcher(VIEW_REGISTER).forward(req,resp);
        }



    }

    boolean passwordsEqual(HttpServletRequest req, HttpServletResponse resp){
        return req.getParameter("password").equals(req.getParameter("repeatedPassword"));
    }
}
